package d1229;

import java.util.HashMap;
import java.util.Map;

class Solution {
    public int countQuadruplets(int[] nums) {
        int len = nums.length;
        Map<Integer, Integer> map = new HashMap<>();
        int res = 0;
        //必须从C为分界点，不然容易导致重复
        for (int c = len - 2; c >= 2; c--) {
            //补充c的下一个值作为d
            //逐步增加d的值，确保在计数时不会重复
            map.put(nums[c + 1], map.getOrDefault(nums[c + 1], 0) + 1);
            //此时a,b可以任意取值,因为a,b都会小于c
            for (int a = 0; a <= c - 2; a++) {
                for (int b = a + 1; b <= c - 1; b++) {
                    res += map.getOrDefault(nums[a] + nums[b] + nums[c], 0);
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = {3,3,6,4,5};
        int res = new Solution().countQuadruplets(nums);
        System.out.println(res);
    }
}